<?php
html::printPath(
    array(
        'viSeminar.form-title' => 'admin/viSeminar/overview',
        $viSeminar['name'] => array(
            'admin/viSeminar/display?id=' . $viSeminar['id'],
            'viSeminar.registrationList-noCount' => 'admin/viSeminar/viRegistrationList?id=' . $viSeminar['id'],
            'viSeminar.seminarBillTable' => 'admin/viSeminar/billTable?id=' . $viSeminar['id']
        ),
        'viSeminar.assignAccomodation-title' => ''
    ),
    'viSeminar.assignAccomodation-desc'
);
$msg->printMessages();

echo form::open('/admin/viSeminarAssignAccomodation/assign');
echo form::hidden('vi_seminar_id', $viSeminar['id']);

function roomNumberCellContent($viRegistration, $entityListField) {
    return form::input('roomNumber_' . $viRegistration['id'], $viRegistration['room_number'], 'onchange="return roomUsage()"');
}

foreach ($viRegistrationList as $viRegistration) {
    echo form::hidden('regIdList[]', $viRegistration['id']);
}
echo form::hiddenWithId('orderName', '');
echo form::hiddenWithId('orderBy', '');
echo form::hiddenWithId('order', '');

$elb = new EntityListBuilder($this, $viRegistrationList);
$elb->setUuid($this->ASSIGN_ACCOMODATION_LIST_UUID);
$elb->setId('accomodationTable')->extraClassNames('accomodationTable');
$elb->integer('id')->labelKey('viRegistration.id-short')->linkWithValue('/admin/viRegistration/display?id=', 'id')->openInPopup();
$elb->text('room_number')->labelKey('viRegistration.room_number')->valueFunction('roomNumberCellContent');
$elb->text('person_id')->doubleValue('last_name', 'first_name')->alignLeft()->orderByField('last_name')->linkWithValue('/admin/person/display?id=', 'person_id')->labelKey('person.full_name');
$elb->text('gender')->labelKey('person.gender')->l10n('person.gender-');
$elb->text('vi_accomodation_type_name')->labelKey('viRegistration.vi_accomodation_type_code');
$elb->date('created_at')->labelKey('viRegistration.registered_on');
$elb->date('birth_date')->labelKey('person.birth_date');
$elb->text('city')->labelKey('person.city');
$elb->longText('notes')->labelKey('viRegistration.notes');
$elb->longText('person_notes')->labelKey('viSeminar.personNotes');
$elb->write();

echo '<p>' . form::submitButton('save', 'button/save.png', 'saveButton') . '</p>';

form::close();
?>

<div id="roomUsageBlock"></div>

<script type="text/javascript">
    var roomNameHeader = '<?php echo text::getEscaped('viRegistration.room_number') ?>';
    var countHeader = '<?php echo text::getEscaped('viSeminar.roomUsage-count') ?>';
    function roomUsage() {
        var counts = new Array();
        var accomodationTable = document.getElementById('accomodationTable');
        $('input[type="text"]', accomodationTable).each(function(i) {
            // document.write(this.value);
            if (! counts[this.value]) {
                counts[this.value] = 0;
            }
            counts[this.value] += 1;
        });

        var html = '<table class="pricesTable"><thead><tr><th>' + roomNameHeader + '</th><th>' + countHeader + '</th></tr></thead>';
        for (room in counts) {
            html += '<tr><th>' + room + '</th><td>' + counts[room] + '</td></tr>';
        }
        html += '</tbody></table>';
        $('#roomUsageBlock').html(html);
        return false;
    }
    roomUsage('accomodationTable', 'roomUsageBlock');

    /**
     * Overrides the method from yosemin.js to save entered room numbers when sorting the table.
     */
    function orderList(orderUuid, orderBy, order) {
        if (! order) {
            order = 'ASC';
        }
        document.getElementById('orderName').value = orderUuid;
        document.getElementById('order').value = order;
        document.getElementById('orderBy').value = orderBy;
        document.getElementById('saveButton').click();
    }
</script>
